/* ====================================================================
 * (#)Header: User.java
 * (#)Revision: 
 * (#)Date Created: Mar 1, 2007 11:34:23 PM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.entity.app;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import com.karolak.entity.Person;

/**
 * This class is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
@Entity
@Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = { "user_name" }))
@PrimaryKeyJoinColumn(name = "id")
public class User extends Person {

	private static final long serialVersionUID = 1L;

	private String userName;
	private String password;
	private Boolean active;
	private Set<Role> roles;

	public User() {
		setRoles(new HashSet<Role>());
	}

	@Basic(fetch = FetchType.EAGER)
	@Column(name = "user_name", /* unique = true, */nullable = false, length = 20)
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Basic(fetch = FetchType.EAGER)
	@Column(nullable = true, length = 40)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Basic(fetch = FetchType.EAGER)
	@Column(nullable = true)
	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = @JoinColumn(name = "role_id"))
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public void addRole(Role role) {
		getRoles().add(role);
	}

	public void removeRole(Role role) {
		getRoles().remove(role);
	}
}